package com.blb.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter("/user/*")
public class UserFilter implements Filter {

//    private String[] urls = {"/user/view/login","/user/view/register"};

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
//
//        //白名单放行
//        String uri = req.getRequestURI();
//        for (String url : urls) {
//            if(uri.contains(url)){
//                filterChain.doFilter(req, resp);
//                return;
//            }
//        }
//
//        //1.获取session对象
//        HttpSession session = req.getSession();
//        //2.从session中获取user标记
//        Object user = session.getAttribute("user");
//        //4.为null则重定向登录页面，否则放行
//        if(user==null){
//            resp.sendRedirect("/user/view/login");
//            return;
//        }
        //放行请求
        filterChain.doFilter(req, resp);
    }

    @Override
    public void destroy() {

    }
}
